package com.fingard.dsp.bank.directbank.gzrcb01;

import org.apache.commons.lang.StringUtils;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.dsp.bank.directbank.jyrb01.JYRB01Header;
import com.fingard.net.*;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Map;

/**
 * Created by F on 2018/5/10.
 */
public class GZRCB01Base extends DirectBase implements ITcpClient {
    @Override
    public void getContentLength(TcpReceiver p_receiver) throws Exception {
        if (p_receiver.contentLength == -1) {
            if (p_receiver.readedLength >= 6) {
                // String tmpStrReq = new
                // String(tmpReceiver.reqBytes,tmpReceiver.contentStartIndex,tmpReceiver.contentLength,tmpReceiver.charsetName);
                String tmpHead = new String(p_receiver.recBytes, 0, 6, getCharset());
                WriteBankLogStep3(tmpHead);
                p_receiver.contentLength = Integer.parseInt(tmpHead.trim())-31;
                p_receiver.contentStartIndex = 31;
            }
        }

    }
    protected String getCharset() {
        return getBankConfig().getCharset("gb18030");
    }

    /**
     *
     * @param xml  报文数据
     * @param opname   操作名称
     * @return
     */
    protected String[] sendToBank(String xml,String opname) throws Exception{
        String header = String.format("%06d%s%s",xml.length()+31,opname,StringUtils.repeat("0",19))+"#";
        xml =header+xml;
        WriteBankLogStep2(xml);
        String result[]={"",""} ;
        TcpPortClient tmpClient = new TcpPortClient(bankFront.serverIP,Integer.valueOf(bankFront.serverPort),this);
        TcpReceiver tcpReceiver = tmpClient.sendMessage(xml.getBytes(getCharset()));
        result[0] = tcpReceiver.connState;
        if(result[0].length()>0){
            result[1] = tcpReceiver.connMsg;
            return result;
        }
        if(tcpReceiver.recBytes!=null){
            result[1] = new String (tcpReceiver.recBytes,0,tcpReceiver.recBytes.length,getCharset() );
        }
        WriteBankLogStep3(result[1]);
        if (result[1].length()>31){
            result[1] = result[1].substring(31);
        }
        return result;
    }
    protected void wirteCommenHeader(XmlTextWriter xmlTextWriter, GZRCB01Header header) {
        xmlTextWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"gb18030\" ?>");
        xmlTextWriter.writeStartElement("msg");
        xmlTextWriter.writeStartElement("comm_head");
        xmlTextWriter.writeElementString("ver_no", header.ver_no);
        xmlTextWriter.writeElementString("bank_id", header.bank_id);
        xmlTextWriter.writeElementString("snd_chnl_no", header.snd_chnl_no);
        xmlTextWriter.writeElementString("rcv_chnl_no", header.rcv_chnl_no);
        xmlTextWriter.writeElementString("ext_txn_no", header.ext_txn_no);
        xmlTextWriter.writeElementString("chnl_dt", header.chnl_dt);
        xmlTextWriter.writeElementString("chnl_tm", header.chnl_tm);
        xmlTextWriter.writeElementString("chnl_seq", header.chnl_seq);
        xmlTextWriter.writeEndElement();
    }

}
